<?php
// $Id: hotel.php 406 2012-05-29 05:30:34Z tomqin@gmail.com $

class Hotel_Action extends Action_Front{
    public $shop_id;

    public function __construct(){
        $this->route = 1;//路由1
    }

    function init(){
        $this->shop_id = intval($this->params);
        $this->seoContent('大连外卖订餐美食网');
    }

    function _home(){
        $hotel_mod = App::module('table_shop', 'table', array());
        $perpage = 20;
        $page = isset($_GET['p'])?intval($_GET['p']):1;
        $search = $order = $_GET;
        $search['shop_type'] = 2;
        $list = $hotel_mod->search($search, $order, $page, $total, $perpage);
        $pages = pages($total, $page, $perpage, '?p={PAGE}&'.http_build_query($search));

        $this->setVar('list', $list);
        $this->setVar('pages', $pages);
        $this->render('hotel-list');
    }

    function _index(){
        $hotel = $this->getHotel($this->shop_id);

        if($hotel){
            $hotel['menus']  = $this->getHotelMenu($this->shop_id);
            $hotel['halls']  = $this->getHotelHall($this->shop_id);
            $hotel['albums'] = $this->getAlbum($this->shop_id);
            $relates         = $this->getRelateHotel(5);

            //seo 信息
            $title = $hotel['name'].($hotel['alias_name']?'('.$hotel['alias_name'].')':'');
            $keywords = '';
            $description = $title.','.$hotel['address'].','.$hotel['topic'].','.description($hotel['description']);
            $this->seoContent($title, $keywords, $description);

            $this->setVar('hotel', $hotel);
            $this->setVar('relates', $relates);
            $this->render('hotel-detail');
        }else{
            exit('酒店不存在！');
        }
    }

    function _picture(){
        $hotel = $this->getHotel($this->shop_id);

        if($hotel){
            $hotel['albums'] = $this->getAlbum($this->shop_id);

            //seo 信息
            $title = $hotel['name'].($hotel['alias_name']?'('.$hotel['alias_name'].')':'').'_相册';
            $keywords = '';
            $description = $title.','.$hotel['address'].','.$hotel['topic'].','.description($hotel['description']);
            $this->seoContent($title, $keywords, $description);

            $this->setVar('hotel', $hotel);
            $this->render('hotel-picture');
        }else{
            exit('酒店不存在！');
        }
    }

    function onIndex(){
        if(!$this->shop_id){
            $this->_home();
        }else{
            $cachename = join('|', array($this->cityid, $this->action, $this->method, $this->shop_id));
            $force = array_get_bool($_GET, 'force');
            $this->showCache($cachename, array($this, '_index'), 86400, $force);
        }
    }

    function onPicture(){
        if(!$this->shop_id){
            $this->_home();
        }else{
            $cachename = join('|', array($this->cityid, $this->action, $this->method, $this->shop_id));
            $force = array_get_bool($_GET, 'force');
            $this->showCache($cachename, array($this, '_picture'), 86400, $force);
        }
    }

    function onPreview(){
        $this->_index(true);
    }

    protected function getHotel($shop_id){
        return Db::getRow("SELECT h.*, s.* FROM `biz_shop` s LEFT JOIN `biz_hotel` h ON s.shop_id = h.shop_id WHERE s.shop_id = '{$shop_id}'");
    }

    protected function getHotelHall($shop_id){
        $res = Db::getAll("SELECT * FROM `biz_hotel_hall` WHERE shop_id = '{$shop_id}' ORDER BY ordnum DESC");
        return $res ? $res : array();
    }

    protected function getHotelMenu($shop_id){
        $res = Db::getAll("SELECT * FROM `biz_hotel_menu` WHERE shop_id = '{$shop_id}' ORDER BY ordnum DESC");
        return $res ? $res : array();
    }

    protected function getAlbum($shop_id){
        $res = Db::getAll("SELECT * FROM `biz_album` WHERE shop_id = '{$shop_id}' ORDER BY ordnum DESC");
        return $res ? $res : array();
    }

    protected function getRelateHotel($num){
        $num = intval($num);
        $res = Db::getAll("SELECT shop_id, picture, name, list_data FROM `biz_shop` WHERE shop_type = 2 AND shop_id <> {$this->shop_id} ORDER BY rand() Limit {$num}");
        return $res ? $res : array();
    }
}
?>